<?php defined('SYSPATH') or die('No direct script access.');

abstract class Controller_Abase_Adefaultactions extends Controller_Abase_Acommon {

    public $template = 'alayout/amain';
    public $obj = NULL;

    public function action_index() {
        $params = $this->params();
        $this->showDefaultForm($params, ORM::factory($this->request->controller()));
        $this->showPagination(); // Пагинация
        $this->showDefaultList(); // Рендерим список данных и форму редактирования\добавления
    }

    public function action_add() {
        $newObj = ORM::factory($this->request->controller());
        $fields = arr::path($this->params(), 'fields'); // Получаем поля сверху

        foreach ($fields as $key => $item) { // Заполняем значения полей
            if (arr::path($item, 'type') == 'image') {
                $newObj->$key = $this->uploadImage($key, NULL);

                if (!empty($item['image_params']['types'])) {
                    $k = $key.'_params';
                    $newObj->$k = $this->request->post($key.'_params');
                }
            } elseif (arr::path($item, 'relation_params.type') != 'HAS_MANY') {
                $newObj->$key = $this->request->post($key);
            }
        }

        $newObj->save();
        $this->redirect('administration/'.$this->request->controller().'/'.$this->request->param('page'));
    }

    public function action_edit() {
        $obj = ORM::factory($this->request->controller(), $this->request->param('id'));

        if ($this->request->param('save')) {
            $fields = arr::path($this->params(), 'fields'); // Получаем поля сверху

            foreach ($fields as $key => $item) { // Заполняем новые значения полей
                if (arr::path($item, 'type') == 'image') {
                    $obj->$key = $this->uploadImage($key, $obj->$key);

                    if (!empty($item['image_params']['types'])) {
                        $k = $key.'_params';
                        $obj->$k = $this->request->post($key.'_params');
                    }
                } elseif (arr::path($item, 'relation_params.type') != 'HAS_MANY') {
                    $obj->$key = $this->request->post($key);
                }
            }

            $obj->save();
            $this->redirect('administration/'.$this->request->controller().'/'.$this->request->param('page').'/edit/'.$this->request->param('id'));
            exit();
        }

        $params = $this->params($obj);
        $this->showDefaultForm($params, $obj, 'edit');
        $this->showPagination(); // Пагинация
        $this->showDefaultList(); // Рендерим список данных и форму редактирования\добавления
    }

    public function action_delete() {
        if ($this->obj == NULL) {
            $this->obj = ORM::factory($this->request->controller(), $this->request->param('id'));
        }

        $params = $this->params($this->obj);

        if (!empty($params['admin_image_field'])) {
            $this->deleteImages($params['admin_image_field'], $this->obj->$params['admin_image_field']);
        }

        $this->obj->delete();
        $this->redirect('administration/'.$this->request->controller().'/'.$this->request->param('page'));
    }

}